

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAFetch.QATdx &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../QAFetch.html">QUANTAXIS.QAFetch</a> &raquo;</li>
        
      <li>QUANTAXIS.QAFetch.QATdx</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAFetch.QATdx</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>
<span class="c1">#</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>
<span class="kn">import</span> <span class="nn">datetime</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">pytdx.exhq</span> <span class="k">import</span> <span class="n">TdxExHq_API</span>
<span class="kn">from</span> <span class="nn">pytdx.hq</span> <span class="k">import</span> <span class="n">TdxHq_API</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil</span> <span class="k">import</span> <span class="p">(</span><span class="n">QA_util_date_stamp</span><span class="p">,</span> <span class="n">QA_util_date_str2int</span><span class="p">,</span>
                              <span class="n">QA_util_date_valid</span><span class="p">,</span> <span class="n">QA_util_get_real_date</span><span class="p">,</span>
                              <span class="n">QA_util_get_real_datelist</span><span class="p">,</span> <span class="n">QA_util_get_trade_gap</span><span class="p">,</span>
                              <span class="n">QA_util_log_info</span><span class="p">,</span> <span class="n">QA_util_time_stamp</span><span class="p">,</span>
                              <span class="n">QA_util_web_ping</span><span class="p">,</span> <span class="n">future_ip_list</span><span class="p">,</span> <span class="n">stock_ip_list</span><span class="p">,</span>
                              <span class="n">trade_date_sse</span><span class="p">)</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAFetch.base</span> <span class="k">import</span> <span class="n">_select_market_code</span><span class="p">,</span> <span class="n">_select_type</span>

<span class="c1"># 基于Pytdx的数据接口,好处是可以在linux/mac上联入通达信行情</span>
<span class="c1"># 具体参见rainx的pytdx(https://github.com/rainx/pytdx)</span>
<span class="c1">#</span>


<div class="viewcode-block" id="ping"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.ping">[docs]</a><span class="k">def</span> <span class="nf">ping</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">7709</span><span class="p">,</span> <span class="n">type_</span><span class="o">=</span><span class="s1">&#39;stock&#39;</span><span class="p">):</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">apix</span> <span class="o">=</span> <span class="n">TdxExHq_API</span><span class="p">()</span>
    <span class="n">__time1</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">type_</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">]:</span>
            <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">time_out</span><span class="o">=</span><span class="mf">0.7</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_list</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">800</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">__time1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;BAD RESPONSE </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ip</span><span class="p">))</span>
                    <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">type_</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">]:</span>
            <span class="k">with</span> <span class="n">apix</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">time_out</span><span class="o">=</span><span class="mf">0.7</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">apix</span><span class="o">.</span><span class="n">get_instrument_count</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">10000</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">__time1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;️Bad FUTUREIP REPSONSE </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ip</span><span class="p">))</span>
                    <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;BAD RESPONSE </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ip</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span></div>


<div class="viewcode-block" id="select_best_ip"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.select_best_ip">[docs]</a><span class="k">def</span> <span class="nf">select_best_ip</span><span class="p">():</span>
    <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;Selecting the Best Server IP of TDX&#39;</span><span class="p">)</span>

    <span class="n">data_stock</span> <span class="o">=</span> <span class="p">[</span><span class="n">ping</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;ip&#39;</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;port&#39;</span><span class="p">],</span> <span class="s1">&#39;stock&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">stock_ip_list</span><span class="p">]</span>
    <span class="n">data_future</span> <span class="o">=</span> <span class="p">[</span><span class="n">ping</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;ip&#39;</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;port&#39;</span><span class="p">],</span> <span class="s1">&#39;future&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">future_ip_list</span><span class="p">]</span>

    <span class="n">best_stock_ip</span> <span class="o">=</span> <span class="n">stock_ip_list</span><span class="p">[</span><span class="n">data_stock</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">data_stock</span><span class="p">))]</span>
    <span class="n">best_future_ip</span> <span class="o">=</span> <span class="n">future_ip_list</span><span class="p">[</span><span class="n">data_future</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">data_future</span><span class="p">))]</span>

    <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;=== The BEST SERVER ===</span><span class="se">\n</span><span class="s1"> stock_ip </span><span class="si">{}</span><span class="s1"> future_ip </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
        <span class="n">best_stock_ip</span><span class="p">[</span><span class="s1">&#39;ip&#39;</span><span class="p">],</span> <span class="n">best_future_ip</span><span class="p">[</span><span class="s1">&#39;ip&#39;</span><span class="p">]))</span>
    <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;stock&#39;</span><span class="p">:</span> <span class="n">best_stock_ip</span><span class="p">,</span> <span class="s1">&#39;future&#39;</span><span class="p">:</span> <span class="n">best_future_ip</span><span class="p">}</span></div>

<span class="k">global</span> <span class="n">best_ip</span>
<span class="n">best_ip</span><span class="o">=</span><span class="p">{</span>
    <span class="s1">&#39;stock&#39;</span><span class="p">:{</span>
        <span class="s1">&#39;ip&#39;</span><span class="p">:</span><span class="kc">None</span><span class="p">,</span><span class="s1">&#39;port&#39;</span><span class="p">:</span><span class="kc">None</span>
    <span class="p">},</span>
    <span class="s1">&#39;future&#39;</span><span class="p">:{</span>
        <span class="s1">&#39;ip&#39;</span><span class="p">:</span><span class="kc">None</span><span class="p">,</span><span class="s1">&#39;port&#39;</span><span class="p">:</span><span class="kc">None</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="c1"># return 1 if sh, 0 if sz</span>


<div class="viewcode-block" id="QA_fetch_get_security_bars"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_security_bars">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_security_bars</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">_type</span><span class="p">,</span> <span class="n">lens</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;按bar长度推算数据</span>

<span class="sd">    Arguments:</span>
<span class="sd">        code {[type]} -- [description]</span>
<span class="sd">        _type {[type]} -- [description]</span>
<span class="sd">        lens {[type]} -- [description]</span>

<span class="sd">    Keyword Arguments:</span>
<span class="sd">        ip {[type]} -- [description] (default: {best_ip})</span>
<span class="sd">        port {[type]} -- [description] (default: {7709})</span>

<span class="sd">    Returns:</span>
<span class="sd">        [type] -- [description]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_bars</span><span class="p">(</span><span class="n">_select_type</span><span class="p">(</span><span class="n">_type</span><span class="p">),</span> <span class="n">_select_market_code</span><span class="p">(</span>
            <span class="n">code</span><span class="p">),</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]),</span> <span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">time_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_time_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="n">_type</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">tail</span><span class="p">(</span><span class="n">lens</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">data</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_day"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_day">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_day</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">,</span> <span class="n">if_fq</span><span class="o">=</span><span class="s1">&#39;00&#39;</span><span class="p">,</span> <span class="n">frequence</span><span class="o">=</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;获取日线及以上级别的数据</span>


<span class="sd">    Arguments:</span>
<span class="sd">        code {str:6} -- code 是一个单独的code 6位长度的str</span>
<span class="sd">        start_date {str:10} -- 10位长度的日期 比如&#39;2017-01-01&#39;</span>
<span class="sd">        end_date {str:10} -- 10位长度的日期 比如&#39;2018-01-01&#39;</span>

<span class="sd">    Keyword Arguments:</span>
<span class="sd">        if_fq {str} -- &#39;00&#39;/&#39;bfq&#39; -- 不复权 &#39;01&#39;/&#39;qfq&#39; -- 前复权 &#39;02&#39;/&#39;hfq&#39; -- 后复权 &#39;03&#39;/&#39;ddqfq&#39; -- 定点前复权 &#39;04&#39;/&#39;ddhfq&#39; --定点后复权</span>
<span class="sd">        frequency {str} -- day/week/month/quarter/year 也可以是简写 D/W/M/Q/Y</span>
<span class="sd">        ip {str} -- [description] (default: None) ip可以通过select_best_ip()函数重新获取</span>
<span class="sd">        port {int} -- [description] (default: {None})</span>


<span class="sd">    Returns:</span>
<span class="sd">        pd.DataFrame/None -- 返回的是dataframe,如果出错比如只获取了一天,而当天停牌,返回None</span>

<span class="sd">    Exception:</span>
<span class="sd">        如果出现网络问题/服务器拒绝, 会出现socket:time out 尝试再次获取/更换ip即可, 本函数不做处理</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">time_out</span><span class="o">=</span><span class="mf">0.7</span><span class="p">):</span>

        <span class="k">if</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="s1">&#39;D&#39;</span><span class="p">,</span> <span class="s1">&#39;DAY&#39;</span><span class="p">,</span> <span class="s1">&#39;Day&#39;</span><span class="p">]:</span>
            <span class="n">frequence</span> <span class="o">=</span> <span class="mi">9</span>
        <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="s1">&#39;W&#39;</span><span class="p">,</span> <span class="s1">&#39;Week&#39;</span><span class="p">,</span> <span class="s1">&#39;week&#39;</span><span class="p">]:</span>
            <span class="n">frequence</span> <span class="o">=</span> <span class="mi">5</span>
        <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;M&#39;</span><span class="p">,</span> <span class="s1">&#39;m&#39;</span><span class="p">,</span> <span class="s1">&#39;Month&#39;</span><span class="p">]:</span>
            <span class="n">frequence</span> <span class="o">=</span> <span class="mi">6</span>
        <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;quarter&#39;</span><span class="p">,</span> <span class="s1">&#39;Q&#39;</span><span class="p">,</span> <span class="s1">&#39;Quarter&#39;</span><span class="p">,</span> <span class="s1">&#39;q&#39;</span><span class="p">]:</span>
            <span class="n">frequence</span> <span class="o">=</span> <span class="mi">10</span>
        <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;Y&#39;</span><span class="p">,</span> <span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;Year&#39;</span><span class="p">]:</span>
            <span class="n">frequence</span> <span class="o">=</span> <span class="mi">11</span>
        <span class="n">start_date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">start_date</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
        <span class="n">today_</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">today_</span><span class="p">)</span>

        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_bars</span><span class="p">(</span><span class="n">frequence</span><span class="p">,</span> <span class="n">_select_market_code</span><span class="p">(</span>
            <span class="n">code</span><span class="p">),</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

        <span class="c1"># 这里的问题是: 如果只取了一天的股票,而当天停牌, 那么就直接返回None了</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">if_fq</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;00&#39;</span><span class="p">,</span> <span class="s1">&#39;bfq&#39;</span><span class="p">]:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>

        <span class="k">elif</span> <span class="n">if_fq</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;01&#39;</span><span class="p">,</span> <span class="s1">&#39;qfq&#39;</span><span class="p">]:</span>

            <span class="n">xdxr_data</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_xdxr</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>
            <span class="n">bfq_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">bfq_data</span> <span class="o">=</span> <span class="n">bfq_data</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span>
                <span class="p">[</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="c1">#</span>
            <span class="k">if</span> <span class="n">xdxr_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">info</span> <span class="o">=</span> <span class="n">xdxr_data</span><span class="p">[</span><span class="n">xdxr_data</span><span class="p">[</span><span class="s1">&#39;category&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>
                <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">bfq_data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;category&#39;</span><span class="p">]]</span>
                                  <span class="p">[</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&#39;ffill&#39;</span><span class="p">)</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span>
                                              <span class="s1">&#39;songzhuangu&#39;</span><span class="p">]][</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span>
                                    <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigujia&#39;</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;songzhuangu&#39;</span><span class="p">])</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span>
                               <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">1</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">cumprod</span><span class="p">()</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
                <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>

                <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]]</span>
                <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span> <span class="s1">&#39;songzhuangu&#39;</span><span class="p">,</span> <span class="s1">&#39;if_trade&#39;</span><span class="p">,</span> <span class="s1">&#39;category&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>

                <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">return</span> <span class="n">bfq_data</span><span class="p">[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">if_fq</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;03&#39;</span><span class="p">,</span> <span class="s1">&#39;ddqfq&#39;</span><span class="p">]:</span>
            <span class="n">xdxr_data</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_xdxr</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>

            <span class="n">info</span> <span class="o">=</span> <span class="n">xdxr_data</span><span class="p">[</span><span class="n">xdxr_data</span><span class="p">[</span><span class="s1">&#39;category&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>

            <span class="n">bfq_data</span> <span class="o">=</span> <span class="n">data</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
                <span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
                <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

            <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">bfq_data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;category&#39;</span><span class="p">]]</span>
                              <span class="p">[</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">end_date</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&#39;ffill&#39;</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span>
                                          <span class="s1">&#39;songzhuangu&#39;</span><span class="p">]][</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">end_date</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span>
                                <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigujia&#39;</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;songzhuangu&#39;</span><span class="p">])</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span>
                           <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">1</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">cumprod</span><span class="p">()</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>

            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]]</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span> <span class="s1">&#39;songzhuangu&#39;</span><span class="p">,</span> <span class="s1">&#39;if_trade&#39;</span><span class="p">,</span> <span class="s1">&#39;category&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span>

        <span class="k">elif</span> <span class="n">if_fq</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;02&#39;</span><span class="p">,</span> <span class="s1">&#39;hfq&#39;</span><span class="p">]:</span>
            <span class="n">xdxr_data</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_xdxr</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>

            <span class="n">info</span> <span class="o">=</span> <span class="n">xdxr_data</span><span class="p">[</span><span class="n">xdxr_data</span><span class="p">[</span><span class="s1">&#39;category&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>

            <span class="n">bfq_data</span> <span class="o">=</span> <span class="n">data</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
                <span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
                <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

            <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">bfq_data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;category&#39;</span><span class="p">]]</span>
                              <span class="p">[</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&#39;ffill&#39;</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span>
                                          <span class="s1">&#39;songzhuangu&#39;</span><span class="p">]][</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span>
                                <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigujia&#39;</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;songzhuangu&#39;</span><span class="p">])</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span>
                           <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">cumprod</span><span class="p">()</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]]</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span> <span class="s1">&#39;songzhuangu&#39;</span><span class="p">,</span> <span class="s1">&#39;if_trade&#39;</span><span class="p">,</span> <span class="s1">&#39;category&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span>

        <span class="k">elif</span> <span class="n">if_fq</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;04&#39;</span><span class="p">,</span> <span class="s1">&#39;ddhfq&#39;</span><span class="p">]:</span>
            <span class="n">xdxr_data</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_xdxr</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>

            <span class="n">info</span> <span class="o">=</span> <span class="n">xdxr_data</span><span class="p">[</span><span class="n">xdxr_data</span><span class="p">[</span><span class="s1">&#39;category&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">]</span>

            <span class="n">bfq_data</span> <span class="o">=</span> <span class="n">data</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
                <span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
                <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

            <span class="n">bfq_data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">bfq_data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;category&#39;</span><span class="p">]]</span>
                              <span class="p">[</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">end_date</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">index</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&#39;ffill&#39;</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">data</span><span class="p">,</span> <span class="n">info</span><span class="p">[[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span>
                                          <span class="s1">&#39;songzhuangu&#39;</span><span class="p">]][</span><span class="n">bfq_data</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">end_date</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">10</span> <span class="o">-</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;fenhong&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span>
                                <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigujia&#39;</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="mi">10</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;peigu&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;songzhuangu&#39;</span><span class="p">])</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span>
                           <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">cumprod</span><span class="p">()</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;high&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;low&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;close&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;preclose&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;adj&#39;</span><span class="p">]</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;if_trade&#39;</span><span class="p">]]</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;fenhong&#39;</span><span class="p">,</span> <span class="s1">&#39;peigu&#39;</span><span class="p">,</span> <span class="s1">&#39;peigujia&#39;</span><span class="p">,</span> <span class="s1">&#39;songzhuangu&#39;</span><span class="p">,</span> <span class="s1">&#39;if_trade&#39;</span><span class="p">,</span> <span class="s1">&#39;category&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;open&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_min"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_min">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_min</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">frequence</span><span class="o">=</span><span class="s1">&#39;1min&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">type_</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">start_date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">start</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
    <span class="n">today_</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
    <span class="n">lens</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">today_</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;5&#39;</span><span class="p">,</span> <span class="s1">&#39;5m&#39;</span><span class="p">,</span> <span class="s1">&#39;5min&#39;</span><span class="p">,</span> <span class="s1">&#39;five&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;5min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">48</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="s1">&#39;1m&#39;</span><span class="p">,</span> <span class="s1">&#39;1min&#39;</span><span class="p">,</span> <span class="s1">&#39;one&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">8</span><span class="p">,</span> <span class="s1">&#39;1min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">240</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;15&#39;</span><span class="p">,</span> <span class="s1">&#39;15m&#39;</span><span class="p">,</span> <span class="s1">&#39;15min&#39;</span><span class="p">,</span> <span class="s1">&#39;fifteen&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;15min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">16</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;30&#39;</span><span class="p">,</span> <span class="s1">&#39;30m&#39;</span><span class="p">,</span> <span class="s1">&#39;30min&#39;</span><span class="p">,</span> <span class="s1">&#39;half&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;30min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;60&#39;</span><span class="p">,</span> <span class="s1">&#39;60m&#39;</span><span class="p">,</span> <span class="s1">&#39;60min&#39;</span><span class="p">,</span> <span class="s1">&#39;1h&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;60min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">if</span> <span class="n">lens</span> <span class="o">&gt;</span> <span class="mi">20800</span><span class="p">:</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">20800</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>

        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_bars</span><span class="p">(</span><span class="n">frequence</span><span class="p">,</span> <span class="n">_select_market_code</span><span class="p">(</span>
            <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)),</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">),</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]),</span> <span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">time_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_time_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="n">type_</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_latest"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_latest">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_latest</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">code</span> <span class="o">=</span> <span class="p">[</span><span class="n">code</span><span class="p">]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">else</span> <span class="n">code</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">(</span><span class="n">multithread</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_bars</span><span class="p">(</span>
            <span class="mi">9</span><span class="p">,</span> <span class="n">_select_market_code</span><span class="p">(</span><span class="n">item</span><span class="p">),</span> <span class="n">item</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="n">item</span><span class="p">)</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">code</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span>
                                        <span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])),</span> <span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span>
                    <span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))))</span>\
            <span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_realtime"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_realtime">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_realtime</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;000001&#39;</span><span class="p">,</span> <span class="s1">&#39;000002&#39;</span><span class="p">],</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">__data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">code</span> <span class="o">=</span> <span class="p">[</span><span class="n">code</span><span class="p">]</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">code</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">str</span> <span class="k">else</span> <span class="n">code</span>
        <span class="k">for</span> <span class="n">id_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">code</span><span class="p">)</span> <span class="o">/</span> <span class="mi">80</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">__data</span> <span class="o">=</span> <span class="n">__data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_quotes</span><span class="p">(</span>
                <span class="p">[(</span><span class="n">_select_market_code</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">code</span><span class="p">[</span><span class="mi">80</span> <span class="o">*</span> <span class="n">id_</span><span class="p">:</span><span class="mi">80</span> <span class="o">*</span> <span class="p">(</span><span class="n">id_</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]])))</span>
            <span class="n">__data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">__data</span><span class="p">[[</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;active1&#39;</span><span class="p">,</span> <span class="s1">&#39;active2&#39;</span><span class="p">,</span> <span class="s1">&#39;last_close&#39;</span><span class="p">,</span> <span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;open&#39;</span><span class="p">,</span> <span class="s1">&#39;high&#39;</span><span class="p">,</span> <span class="s1">&#39;low&#39;</span><span class="p">,</span> <span class="s1">&#39;price&#39;</span><span class="p">,</span> <span class="s1">&#39;cur_vol&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;s_vol&#39;</span><span class="p">,</span> <span class="s1">&#39;b_vol&#39;</span><span class="p">,</span> <span class="s1">&#39;vol&#39;</span><span class="p">,</span> <span class="s1">&#39;ask1&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol1&#39;</span><span class="p">,</span> <span class="s1">&#39;bid1&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol1&#39;</span><span class="p">,</span> <span class="s1">&#39;ask2&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol2&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;bid2&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol2&#39;</span><span class="p">,</span> <span class="s1">&#39;ask3&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol3&#39;</span><span class="p">,</span> <span class="s1">&#39;bid3&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol3&#39;</span><span class="p">,</span> <span class="s1">&#39;ask4&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;ask_vol4&#39;</span><span class="p">,</span> <span class="s1">&#39;bid4&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol4&#39;</span><span class="p">,</span> <span class="s1">&#39;ask5&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol5&#39;</span><span class="p">,</span> <span class="s1">&#39;bid5&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol5&#39;</span><span class="p">]]</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_fetch_depth_market_data"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_depth_market_data">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_depth_market_data</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;000001&#39;</span><span class="p">,</span> <span class="s1">&#39;000002&#39;</span><span class="p">],</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">__data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">code</span> <span class="o">=</span> <span class="p">[</span><span class="n">code</span><span class="p">]</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">code</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">str</span> <span class="k">else</span> <span class="n">code</span>
        <span class="k">for</span> <span class="n">id_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">code</span><span class="p">)</span> <span class="o">/</span> <span class="mi">80</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">__data</span> <span class="o">=</span> <span class="n">__data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_quotes</span><span class="p">(</span>
                <span class="p">[(</span><span class="n">_select_market_code</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">code</span><span class="p">[</span><span class="mi">80</span> <span class="o">*</span> <span class="n">id_</span><span class="p">:</span><span class="mi">80</span> <span class="o">*</span> <span class="p">(</span><span class="n">id_</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]])))</span>
            <span class="n">__data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">__data</span><span class="p">[[</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;active1&#39;</span><span class="p">,</span> <span class="s1">&#39;active2&#39;</span><span class="p">,</span> <span class="s1">&#39;last_close&#39;</span><span class="p">,</span> <span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;open&#39;</span><span class="p">,</span> <span class="s1">&#39;high&#39;</span><span class="p">,</span> <span class="s1">&#39;low&#39;</span><span class="p">,</span> <span class="s1">&#39;price&#39;</span><span class="p">,</span> <span class="s1">&#39;cur_vol&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;s_vol&#39;</span><span class="p">,</span> <span class="s1">&#39;b_vol&#39;</span><span class="p">,</span> <span class="s1">&#39;vol&#39;</span><span class="p">,</span> <span class="s1">&#39;ask1&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol1&#39;</span><span class="p">,</span> <span class="s1">&#39;bid1&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol1&#39;</span><span class="p">,</span> <span class="s1">&#39;ask2&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol2&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;bid2&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol2&#39;</span><span class="p">,</span> <span class="s1">&#39;ask3&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol3&#39;</span><span class="p">,</span> <span class="s1">&#39;bid3&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol3&#39;</span><span class="p">,</span> <span class="s1">&#39;ask4&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;ask_vol4&#39;</span><span class="p">,</span> <span class="s1">&#39;bid4&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol4&#39;</span><span class="p">,</span> <span class="s1">&#39;ask5&#39;</span><span class="p">,</span> <span class="s1">&#39;ask_vol5&#39;</span><span class="p">,</span> <span class="s1">&#39;bid5&#39;</span><span class="p">,</span> <span class="s1">&#39;bid_vol5&#39;</span><span class="p">]]</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="s1">&#39;code&#39;</span><span class="p">],</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>


<span class="sd">&#39;&#39;&#39;</span>
<span class="sd">沪市</span>
<span class="sd">001×××国债现货；</span>
<span class="sd">110×××120×××企业债券；</span>
<span class="sd">129×××100×××可转换债券；</span>
<span class="sd">201×××国债回购；</span>
<span class="sd">310×××国债期货；</span>
<span class="sd">500×××550×××基金；</span>


<span class="sd">600×××A股；</span>

<span class="sd">700×××配股；</span>
<span class="sd">710×××转配股；</span>
<span class="sd">701×××转配股再配股；</span>
<span class="sd">711×××转配股再转配股；</span>
<span class="sd">720×××红利；</span>
<span class="sd">730×××新股申购；</span>
<span class="sd">735×××新基金申购；</span>
<span class="sd">737×××新股配售；</span>
<span class="sd">900×××B股。</span>

<span class="sd">深市</span>
<span class="sd">深市A股票买卖的代码是以000打头，如：顺鑫农业：股票代码是000860。</span>
<span class="sd">B股买卖的代码是以200打头，如：深中冠B股，代码是200018。</span>
<span class="sd">中小板股票代码以002打头，如：东华合创股票代码是002065。</span>
<span class="sd">创业板股票代码以300打头，如：探路者股票代码是：300005</span>


<span class="sd">更多参见 issue https://github.com/QUANTAXIS/QUANTAXIS/issues/158</span>
<span class="sd">@yutiansut</span>
<span class="sd">&#39;&#39;&#39;</span>


<div class="viewcode-block" id="for_sz"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.for_sz">[docs]</a><span class="k">def</span> <span class="nf">for_sz</span><span class="p">(</span><span class="n">code</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;00&#39;</span><span class="p">,</span> <span class="s1">&#39;30&#39;</span><span class="p">,</span> <span class="s1">&#39;02&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="s1">&#39;stock_cn&#39;</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;39&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="s1">&#39;index_cn&#39;</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;15&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="s1">&#39;etf_cn&#39;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;undefined&#39;</span></div>


<div class="viewcode-block" id="for_sh"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.for_sh">[docs]</a><span class="k">def</span> <span class="nf">for_sh</span><span class="p">(</span><span class="n">code</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;6&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;stock_cn&#39;</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;0&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;index_cn&#39;</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;51&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;etf_cn&#39;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;undefined&#39;</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_list"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_list">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_list</span><span class="p">(</span><span class="n">type_</span><span class="o">=</span><span class="s1">&#39;stock&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_list</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">sse</span><span class="o">=</span><span class="s1">&#39;sz&#39;</span> <span class="k">if</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span> <span class="s1">&#39;sh&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span>
            <span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;sse&#39;</span><span class="p">],</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_count</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="c1">#data.code = data.code.apply(int)</span>
        <span class="n">sz</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;sse==&quot;sz&quot;&#39;</span><span class="p">)</span>
        <span class="n">sh</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;sse==&quot;sh&quot;&#39;</span><span class="p">)</span>

        <span class="n">sz</span> <span class="o">=</span> <span class="n">sz</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">sec</span><span class="o">=</span><span class="n">sz</span><span class="o">.</span><span class="n">code</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">for_sz</span><span class="p">))</span>
        <span class="n">sh</span> <span class="o">=</span> <span class="n">sh</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">sec</span><span class="o">=</span><span class="n">sh</span><span class="o">.</span><span class="n">code</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">for_sh</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">type_</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">,</span> <span class="s1">&#39;gp&#39;</span><span class="p">]:</span>

            <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">sz</span><span class="p">,</span> <span class="n">sh</span><span class="p">])</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;sec==&quot;stock_cn&quot;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">6</span><span class="p">]))</span>

        <span class="k">elif</span> <span class="n">type_</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;index&#39;</span><span class="p">,</span> <span class="s1">&#39;zs&#39;</span><span class="p">]:</span>

            <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">sz</span><span class="p">,</span> <span class="n">sh</span><span class="p">])</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;sec==&quot;index_cn&quot;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">6</span><span class="p">]))</span>
            <span class="c1">#.assign(szm=data[&#39;name&#39;].apply(lambda x: &#39;&#39;.join([y[0] for y in lazy_pinyin(x)])))\</span>
            <span class="c1">#.assign(quanpin=data[&#39;name&#39;].apply(lambda x: &#39;&#39;.join(lazy_pinyin(x))))</span>
        <span class="k">elif</span> <span class="n">type_</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;etf&#39;</span><span class="p">,</span> <span class="s1">&#39;ETF&#39;</span><span class="p">]:</span>
            <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">sz</span><span class="p">,</span> <span class="n">sh</span><span class="p">])</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;sec==&quot;etf_cn&quot;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">6</span><span class="p">]))</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">6</span><span class="p">]))</span></div>
            <span class="c1">#.assign(szm=data[&#39;name&#39;].apply(lambda x: &#39;&#39;.join([y[0] for y in lazy_pinyin(x)])))\</span>
            <span class="c1">#    .assign(quanpin=data[&#39;name&#39;].apply(lambda x: &#39;&#39;.join(lazy_pinyin(x))))</span>


<div class="viewcode-block" id="QA_fetch_get_index_day"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_index_day">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_index_day</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">,</span> <span class="n">frequence</span><span class="o">=</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;指数日线&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="s1">&#39;D&#39;</span><span class="p">,</span> <span class="s1">&#39;DAY&#39;</span><span class="p">,</span> <span class="s1">&#39;Day&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span> <span class="o">=</span> <span class="mi">9</span>
    <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="s1">&#39;W&#39;</span><span class="p">,</span> <span class="s1">&#39;Week&#39;</span><span class="p">,</span> <span class="s1">&#39;week&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span> <span class="o">=</span> <span class="mi">5</span>
    <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;M&#39;</span><span class="p">,</span> <span class="s1">&#39;m&#39;</span><span class="p">,</span> <span class="s1">&#39;Month&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span> <span class="o">=</span> <span class="mi">6</span>
    <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;Q&#39;</span><span class="p">,</span> <span class="s1">&#39;Quarter&#39;</span><span class="p">,</span> <span class="s1">&#39;q&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span> <span class="o">=</span> <span class="mi">10</span>
    <span class="k">elif</span> <span class="n">frequence</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;Y&#39;</span><span class="p">,</span> <span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;Year&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span> <span class="o">=</span> <span class="mi">11</span>

    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>

        <span class="n">start_date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">start_date</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
        <span class="n">today_</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">today_</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;5&#39;</span><span class="p">,</span> <span class="s1">&#39;1&#39;</span><span class="p">]:</span>  <span class="c1"># ETF</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_bars</span><span class="p">(</span>
                <span class="n">frequence</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="s1">&#39;8&#39;</span><span class="p">,</span> <span class="s1">&#39;9&#39;</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">]</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_index_bars</span><span class="p">(</span>
                <span class="n">frequence</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="s1">&#39;8&#39;</span><span class="p">,</span> <span class="s1">&#39;9&#39;</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">]</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span>\
            <span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="n">code</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span>
                   <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span></div>


<div class="viewcode-block" id="QA_fetch_get_index_min"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_index_min">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_index_min</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">frequence</span><span class="o">=</span><span class="s1">&#39;1min&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;指数分钟线&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">type_</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="n">start_date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">start</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
    <span class="n">today_</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
    <span class="n">lens</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">today_</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;5&#39;</span><span class="p">,</span> <span class="s1">&#39;5m&#39;</span><span class="p">,</span> <span class="s1">&#39;5min&#39;</span><span class="p">,</span> <span class="s1">&#39;five&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;5min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">48</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="s1">&#39;1m&#39;</span><span class="p">,</span> <span class="s1">&#39;1min&#39;</span><span class="p">,</span> <span class="s1">&#39;one&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">8</span><span class="p">,</span> <span class="s1">&#39;1min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">240</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;15&#39;</span><span class="p">,</span> <span class="s1">&#39;15m&#39;</span><span class="p">,</span> <span class="s1">&#39;15min&#39;</span><span class="p">,</span> <span class="s1">&#39;fifteen&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;15min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">16</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;30&#39;</span><span class="p">,</span> <span class="s1">&#39;30m&#39;</span><span class="p">,</span> <span class="s1">&#39;30min&#39;</span><span class="p">,</span> <span class="s1">&#39;half&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;30min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;60&#39;</span><span class="p">,</span> <span class="s1">&#39;60m&#39;</span><span class="p">,</span> <span class="s1">&#39;60min&#39;</span><span class="p">,</span> <span class="s1">&#39;1h&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;60min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">lens</span>

    <span class="k">if</span> <span class="n">lens</span> <span class="o">&gt;</span> <span class="mi">20800</span><span class="p">:</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">20800</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>

        <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;5&#39;</span><span class="p">,</span> <span class="s1">&#39;1&#39;</span><span class="p">]:</span>  <span class="c1"># ETF</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_security_bars</span><span class="p">(</span>
                <span class="n">frequence</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="s1">&#39;8&#39;</span><span class="p">,</span> <span class="s1">&#39;9&#39;</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">]</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_index_bars</span><span class="p">(</span>
                <span class="n">frequence</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="s1">&#39;8&#39;</span><span class="p">,</span> <span class="s1">&#39;9&#39;</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">]</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">800</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">800</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]),</span> <span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="n">code</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">time_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_time_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="n">type_</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">]</span>
        <span class="c1"># data</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span></div>


<span class="k">def</span> <span class="nf">__QA_fetch_get_stock_transaction</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">day</span><span class="p">,</span> <span class="n">retry</span><span class="p">,</span> <span class="n">api</span><span class="p">):</span>
    <span class="n">batch_size</span> <span class="o">=</span> <span class="mi">2000</span>  <span class="c1"># 800 or 2000 ? 2000 maybe also works</span>
    <span class="n">data_arr</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">max_offset</span> <span class="o">=</span> <span class="mi">21</span>
    <span class="n">cur_offset</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">cur_offset</span> <span class="o">&lt;=</span> <span class="n">max_offset</span><span class="p">:</span>
        <span class="n">one_chunk</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">get_history_transaction_data</span><span class="p">(</span>
            <span class="n">_select_market_code</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)),</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">),</span> <span class="n">cur_offset</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">QA_util_date_str2int</span><span class="p">(</span><span class="n">day</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">one_chunk</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">one_chunk</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">break</span>
        <span class="n">data_arr</span> <span class="o">=</span> <span class="n">one_chunk</span> <span class="o">+</span> <span class="n">data_arr</span>
        <span class="n">cur_offset</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="n">data_</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">data_arr</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">retry</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">__QA_fetch_get_stock_transaction</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">day</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">api</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">data_</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">day</span><span class="p">)</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data_</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">day</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">x</span><span class="p">)))</span>\
                        <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data_</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>


<div class="viewcode-block" id="QA_fetch_get_stock_transaction"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_transaction">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_transaction</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">retry</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;历史逐笔成交 buyorsell 1--sell 0--buy 2--盘前&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>

    <span class="n">real_start</span><span class="p">,</span> <span class="n">real_end</span> <span class="o">=</span> <span class="n">QA_util_get_real_datelist</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">real_start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="n">real_id_range</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">index_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">trade_date_sse</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">real_start</span><span class="p">),</span> <span class="n">trade_date_sse</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">real_end</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">data_</span> <span class="o">=</span> <span class="n">__QA_fetch_get_stock_transaction</span><span class="p">(</span>
                    <span class="n">code</span><span class="p">,</span> <span class="n">trade_date_sse</span><span class="p">[</span><span class="n">index_</span><span class="p">],</span> <span class="n">retry</span><span class="p">,</span> <span class="n">api</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">None</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;Wrong in Getting </span><span class="si">{}</span><span class="s1"> history transaction data in day </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">code</span><span class="p">,</span> <span class="n">trade_date_sse</span><span class="p">[</span><span class="n">index_</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;Successfully Getting </span><span class="si">{}</span><span class="s1"> history transaction data in day </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">code</span><span class="p">,</span> <span class="n">trade_date_sse</span><span class="p">[</span><span class="n">index_</span><span class="p">]))</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data_</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>

            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">19</span><span class="p">]))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_transaction_realtime"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_transaction_realtime">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_transaction_realtime</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;实时逐笔成交 包含集合竞价 buyorsell 1--sell 0--buy 2--盘前&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_transaction_data</span><span class="p">(</span>
                <span class="n">_select_market_code</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">)),</span> <span class="n">code</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">2000</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="k">if</span> <span class="s1">&#39;value&#39;</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;value&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">dropna</span><span class="p">()</span>
            <span class="n">day</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">day</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">day</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">))))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_xdxr"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_xdxr">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_xdxr</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;除权除息&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">market_code</span> <span class="o">=</span> <span class="n">_select_market_code</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">category</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;1&#39;</span><span class="p">:</span> <span class="s1">&#39;除权除息&#39;</span><span class="p">,</span> <span class="s1">&#39;2&#39;</span><span class="p">:</span> <span class="s1">&#39;送配股上市&#39;</span><span class="p">,</span> <span class="s1">&#39;3&#39;</span><span class="p">:</span> <span class="s1">&#39;非流通股上市&#39;</span><span class="p">,</span> <span class="s1">&#39;4&#39;</span><span class="p">:</span> <span class="s1">&#39;未知股本变动&#39;</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">:</span> <span class="s1">&#39;股本变化&#39;</span><span class="p">,</span>
            <span class="s1">&#39;6&#39;</span><span class="p">:</span> <span class="s1">&#39;增发新股&#39;</span><span class="p">,</span> <span class="s1">&#39;7&#39;</span><span class="p">:</span> <span class="s1">&#39;股份回购&#39;</span><span class="p">,</span> <span class="s1">&#39;8&#39;</span><span class="p">:</span> <span class="s1">&#39;增发新股上市&#39;</span><span class="p">,</span> <span class="s1">&#39;9&#39;</span><span class="p">:</span> <span class="s1">&#39;转配股上市&#39;</span><span class="p">,</span> <span class="s1">&#39;10&#39;</span><span class="p">:</span> <span class="s1">&#39;可转债上市&#39;</span><span class="p">,</span>
            <span class="s1">&#39;11&#39;</span><span class="p">:</span> <span class="s1">&#39;扩缩股&#39;</span><span class="p">,</span> <span class="s1">&#39;12&#39;</span><span class="p">:</span> <span class="s1">&#39;非流通股缩股&#39;</span><span class="p">,</span> <span class="s1">&#39;13&#39;</span><span class="p">:</span>  <span class="s1">&#39;送认购权证&#39;</span><span class="p">,</span> <span class="s1">&#39;14&#39;</span><span class="p">:</span> <span class="s1">&#39;送认沽权证&#39;</span><span class="p">}</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_xdxr_info</span><span class="p">(</span><span class="n">market_code</span><span class="p">,</span> <span class="n">code</span><span class="p">))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[[</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">]]))</span>\
                <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">category_meaning</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;category&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">category</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)]))</span>\
                <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
                <span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;panhouliutong&#39;</span><span class="p">:</span> <span class="s1">&#39;liquidity_after&#39;</span><span class="p">,</span>
                                            <span class="s1">&#39;panqianliutong&#39;</span><span class="p">:</span> <span class="s1">&#39;liquidity_before&#39;</span><span class="p">,</span> <span class="s1">&#39;houzongguben&#39;</span><span class="p">:</span> <span class="s1">&#39;shares_after&#39;</span><span class="p">,</span>
                                            <span class="s1">&#39;qianzongguben&#39;</span><span class="p">:</span> <span class="s1">&#39;shares_before&#39;</span><span class="p">})</span>\
                <span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_info"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_info">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_info</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;股票基本信息&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="n">market_code</span> <span class="o">=</span> <span class="n">_select_market_code</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_finance_info</span><span class="p">(</span><span class="n">market_code</span><span class="p">,</span> <span class="n">code</span><span class="p">))</span></div>


<div class="viewcode-block" id="QA_fetch_get_stock_block"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_stock_block">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_stock_block</span><span class="p">(</span><span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;板块数据&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;stock&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">api</span> <span class="o">=</span> <span class="n">TdxHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">api</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>

        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_and_parse_block_info</span><span class="p">(</span><span class="s2">&quot;block_gn.dat&quot;</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s1">&#39;gn&#39;</span><span class="p">),</span>
                          <span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_and_parse_block_info</span><span class="p">(</span>
                              <span class="s2">&quot;block.dat&quot;</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s1">&#39;yb&#39;</span><span class="p">),</span>
                          <span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_and_parse_block_info</span><span class="p">(</span>
                              <span class="s2">&quot;block_zs.dat&quot;</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s1">&#39;zs&#39;</span><span class="p">),</span>
                          <span class="n">api</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">get_and_parse_block_info</span><span class="p">(</span><span class="s2">&quot;block_fg.dat&quot;</span><span class="p">))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="s1">&#39;fg&#39;</span><span class="p">)])</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">10</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">source</span><span class="o">=</span><span class="s1">&#39;tdx&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;block_type&#39;</span><span class="p">,</span> <span class="s1">&#39;code_index&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;Wrong with fetch block &#39;</span><span class="p">)</span></div>


<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">期货数据接口</span>

<span class="sd">1: 获取市场代码</span>
<span class="sd">可以获取该api服务器可以使用的市场列表，类别等信息</span>
<span class="sd">api.get_markets()</span>
<span class="sd">返回结果 api.to_df(api.get_markets()) 一般某个服务器返回的类型比较固定，该结果可以缓存到本地或者内存中。</span>
<span class="sd">2017-07-31 21:22:06,067 - PYTDX - INFO - 获取市场代码</span>
<span class="sd">    market  category    name short_name</span>
<span class="sd">0        1         1     临时股         TP</span>
<span class="sd">1        4        12  郑州商品期权         OZ</span>
<span class="sd">2        5        12  大连商品期权         OD</span>
<span class="sd">3        6        12  上海商品期权         OS</span>
<span class="sd">4        8        12  上海个股期权         QQ</span>
<span class="sd">5       27         5    香港指数         FH</span>
<span class="sd">6       28         3    郑州商品         QZ</span>
<span class="sd">7       29         3    大连商品         QD</span>
<span class="sd">8       30         3    上海期货         QS</span>
<span class="sd">9       31         2    香港主板         KH</span>
<span class="sd">10      32         2    香港权证         KR</span>
<span class="sd">11      33         8   开放式基金         FU</span>
<span class="sd">12      34         9   货币型基金         FB</span>
<span class="sd">13      35         8  招商理财产品         LC</span>
<span class="sd">14      36         9  招商货币产品         LB</span>
<span class="sd">15      37        11    国际指数         FW</span>
<span class="sd">16      38        10  国内宏观指标         HG</span>
<span class="sd">17      40        11   中国概念股         CH</span>
<span class="sd">18      41        11  美股知名公司         MG</span>
<span class="sd">19      43         1   B股转H股         HB</span>
<span class="sd">20      44         1    股份转让         SB</span>
<span class="sd">21      47         3    股指期货         CZ</span>
<span class="sd">22      48         2   香港创业板         KG</span>
<span class="sd">23      49         2  香港信托基金         KT</span>
<span class="sd">24      54         6   国债预发行         GY</span>
<span class="sd">25      60         3  主力期货合约         MA</span>
<span class="sd">26      62         5    中证指数         ZZ</span>
<span class="sd">27      71         2     港股通         GH</span>
<span class="sd">2: 查询代码列表</span>
<span class="sd">参数， 起始位置， 获取数量</span>
<span class="sd">api.get_instrument_info(0, 100)</span>
<span class="sd">Demo: get_list_demo</span>
<span class="sd">3: 查询市场中商品数量</span>
<span class="sd">api.get_instrument_count()</span>
<span class="sd">4: 查询五档行情</span>
<span class="sd">参数 市场ID，证券代码</span>
<span class="sd">市场ID可以通过 get_markets 获得</span>
<span class="sd">api.get_instrument_quote(47, &quot;IF1709&quot;)</span>
<span class="sd">5: 查询分时行情</span>
<span class="sd">参数 市场ID，证券代码</span>
<span class="sd">市场ID可以通过 get_markets 获得</span>
<span class="sd">api.get_minute_time_data(47, &quot;IF1709&quot;)</span>
<span class="sd">6: 查询历史分时行情</span>
<span class="sd">参数 市场ID，证券代码，日期</span>
<span class="sd">市场ID可以通过 get_markets 获得</span>
<span class="sd">日期格式 YYYYMMDD 如 20170811</span>
<span class="sd">api.get_history_minute_time_data(31, &quot;00020&quot;, 20170811)</span>
<span class="sd">7: 查询k线数据</span>
<span class="sd">参数： K线周期， 市场ID， 证券代码，起始位置， 数量</span>
<span class="sd">K线周期参考 TDXParams</span>
<span class="sd">市场ID可以通过 get_markets 获得</span>
<span class="sd">api.get_instrument_bars(TDXParams.KLINE_TYPE_DAILY, 8, &quot;10000843&quot;, 0, 100)</span>
<span class="sd">8: 查询分笔成交</span>
<span class="sd">参数：市场ID，证券代码</span>
<span class="sd">市场ID可以通过 get_markets 获得</span>
<span class="sd">api.get_transaction_data(31, &quot;00020&quot;)</span>
<span class="sd">注意，这个接口最多返回1800条记录, 如果有超过1800条记录的请求，我们有一个start 参数作为便宜量，可以取出超过1800条记录</span>
<span class="sd">如期货的数据：这个接口可以取出1800条之前的记录，数量也是1800条</span>
<span class="sd">api.get_history_transaction_data(47, &quot;IFL0&quot;, 20170810, start=1800)</span>
<span class="sd">9: 查询历史分笔成交</span>
<span class="sd">参数：市场ID，证券代码, 日期</span>
<span class="sd">市场ID可以通过 get_markets 获得</span>
<span class="sd">日期格式 YYYYMMDD 如 20170810</span>
<span class="sd">api.get_history_transaction_data(31, &quot;00020&quot;, 20170810)</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">期货及扩展行情</span>

<span class="sd">首先会初始化/存储一个代码对应表 extension_market_info</span>

<span class="sd">&quot;&quot;&quot;</span>


<div class="viewcode-block" id="QA_fetch_get_future_list"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_future_list">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_future_list</span><span class="p">(</span><span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;期货代码list&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">apix</span> <span class="o">=</span> <span class="n">TdxExHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">apix</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">market_info</span> <span class="o">=</span> <span class="n">apix</span><span class="o">.</span><span class="n">get_markets</span><span class="p">()</span>
        <span class="n">num</span> <span class="o">=</span> <span class="n">apix</span><span class="o">.</span><span class="n">get_instrument_count</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">apix</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span>
            <span class="n">apix</span><span class="o">.</span><span class="n">get_instrument_info</span><span class="p">((</span><span class="nb">int</span><span class="p">(</span><span class="n">num</span> <span class="o">/</span> <span class="mi">500</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">500</span><span class="p">,</span> <span class="mi">500</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">num</span> <span class="o">/</span> <span class="mi">500</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>


<span class="k">global</span> <span class="n">extension_market_info</span>
<span class="n">extension_market_info</span> <span class="o">=</span> <span class="kc">None</span>


<div class="viewcode-block" id="QA_fetch_get_future_day"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_future_day">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_future_day</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">,</span> <span class="n">frequence</span><span class="o">=</span><span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;期货数据 日线&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">apix</span> <span class="o">=</span> <span class="n">TdxExHq_API</span><span class="p">()</span>
    <span class="n">start_date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">start_date</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
    <span class="n">today_</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
    <span class="n">lens</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">today_</span><span class="p">)</span>
    <span class="k">global</span> <span class="n">extension_market_info</span>
    <span class="n">extension_market_info</span> <span class="o">=</span> <span class="n">QA_fetch_get_future_list</span><span class="p">(</span>
    <span class="p">)</span> <span class="k">if</span> <span class="n">extension_market_info</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">extension_market_info</span>

    <span class="k">with</span> <span class="n">apix</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">code_market</span> <span class="o">=</span> <span class="n">extension_market_info</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;code==&quot;</span><span class="si">{}</span><span class="s1">&quot;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>

        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">apix</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">apix</span><span class="o">.</span><span class="n">get_instrument_bars</span><span class="p">(</span><span class="n">_select_type</span><span class="p">(</span>
            <span class="n">frequence</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">code_market</span><span class="o">.</span><span class="n">market</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">),</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">700</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">700</span><span class="p">,</span> <span class="mi">700</span><span class="p">))</span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">700</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])))</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;datetime&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="n">start_date</span><span class="p">:</span><span class="n">end_date</span><span class="p">]</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span></div>


<div class="viewcode-block" id="QA_fetch_get_future_min"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_future_min">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_future_min</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">frequence</span><span class="o">=</span><span class="s1">&#39;1min&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;期货数据 分钟线&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">apix</span> <span class="o">=</span> <span class="n">TdxExHq_API</span><span class="p">()</span>
    <span class="n">type_</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">start_date</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">start</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
    <span class="n">today_</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
    <span class="n">lens</span> <span class="o">=</span> <span class="n">QA_util_get_trade_gap</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">today_</span><span class="p">)</span>
    <span class="k">global</span> <span class="n">extension_market_info</span>
    <span class="n">extension_market_info</span> <span class="o">=</span> <span class="n">QA_fetch_get_future_list</span><span class="p">(</span>
    <span class="p">)</span> <span class="k">if</span> <span class="n">extension_market_info</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">extension_market_info</span>

    <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;5&#39;</span><span class="p">,</span> <span class="s1">&#39;5m&#39;</span><span class="p">,</span> <span class="s1">&#39;5min&#39;</span><span class="p">,</span> <span class="s1">&#39;five&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;5min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">48</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="s1">&#39;1m&#39;</span><span class="p">,</span> <span class="s1">&#39;1min&#39;</span><span class="p">,</span> <span class="s1">&#39;one&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">8</span><span class="p">,</span> <span class="s1">&#39;1min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">240</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;15&#39;</span><span class="p">,</span> <span class="s1">&#39;15m&#39;</span><span class="p">,</span> <span class="s1">&#39;15min&#39;</span><span class="p">,</span> <span class="s1">&#39;fifteen&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;15min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">16</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;30&#39;</span><span class="p">,</span> <span class="s1">&#39;30m&#39;</span><span class="p">,</span> <span class="s1">&#39;30min&#39;</span><span class="p">,</span> <span class="s1">&#39;half&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;30min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">elif</span> <span class="nb">str</span><span class="p">(</span><span class="n">frequence</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;60&#39;</span><span class="p">,</span> <span class="s1">&#39;60m&#39;</span><span class="p">,</span> <span class="s1">&#39;60min&#39;</span><span class="p">,</span> <span class="s1">&#39;1h&#39;</span><span class="p">]:</span>
        <span class="n">frequence</span><span class="p">,</span> <span class="n">type_</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;60min&#39;</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">lens</span>
    <span class="k">if</span> <span class="n">lens</span> <span class="o">&gt;</span> <span class="mi">20800</span><span class="p">:</span>
        <span class="n">lens</span> <span class="o">=</span> <span class="mi">20800</span>
    <span class="k">with</span> <span class="n">apix</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="n">code_market</span> <span class="o">=</span> <span class="n">extension_market_info</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&#39;code==&quot;</span><span class="si">{}</span><span class="s1">&quot;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">apix</span><span class="o">.</span><span class="n">to_df</span><span class="p">(</span><span class="n">apix</span><span class="o">.</span><span class="n">get_instrument_bars</span><span class="p">(</span><span class="n">frequence</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">code_market</span><span class="o">.</span><span class="n">market</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span>
            <span class="n">code</span><span class="p">),</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">700</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="mi">700</span><span class="p">,</span> <span class="mi">700</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">lens</span> <span class="o">/</span> <span class="mi">700</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]),</span> <span class="n">code</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">code</span><span class="p">))</span>\
            <span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_date_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">time_stamp</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">QA_util_time_stamp</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span>\
            <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="n">type_</span><span class="p">)</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&#39;datetime&#39;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)[</span><span class="n">start</span><span class="p">:</span><span class="n">end</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">datetime</span><span class="o">=</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;datetime&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)))</span></div>


<div class="viewcode-block" id="QA_fetch_get_future_transaction"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_future_transaction">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_future_transaction</span><span class="p">(</span><span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;期货历史成交分笔&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">apix</span> <span class="o">=</span> <span class="n">TdxExHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">apix</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="k">pass</span></div>


<div class="viewcode-block" id="QA_fetch_get_future_transaction_realtime"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_future_transaction_realtime">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_future_transaction_realtime</span><span class="p">(</span><span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;期货历史成交分笔&#39;</span>
    <span class="k">global</span> <span class="n">best_ip</span>
    <span class="k">if</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">best_ip</span> <span class="o">=</span> <span class="n">select_best_ip</span><span class="p">()</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">ip</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">port</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">ip</span><span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;ip&#39;</span><span class="p">]</span>
        <span class="n">port</span> <span class="o">=</span> <span class="n">best_ip</span><span class="p">[</span><span class="s1">&#39;future&#39;</span><span class="p">][</span><span class="s1">&#39;port&#39;</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="n">apix</span> <span class="o">=</span> <span class="n">TdxExHq_API</span><span class="p">()</span>
    <span class="k">with</span> <span class="n">apix</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
        <span class="k">pass</span></div>


<div class="viewcode-block" id="QA_fetch_get_future_realtime"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_future_realtime">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_future_realtime</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">ip</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s1">&#39;期货实时价格&#39;</span>
    <span class="k">pass</span></div>


<div class="viewcode-block" id="QA_fetch_get_wholemarket_list"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAFetch.html#QUANTAXIS.QAFetch.QATdx.QA_fetch_get_wholemarket_list">[docs]</a><span class="k">def</span> <span class="nf">QA_fetch_get_wholemarket_list</span><span class="p">():</span>
    <span class="n">hq_codelist</span> <span class="o">=</span> <span class="n">QA_fetch_get_stock_list</span><span class="p">(</span>
        <span class="n">type_</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">],</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
    <span class="n">kz_codelist</span> <span class="o">=</span> <span class="n">QA_fetch_get_future_list</span><span class="p">()</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span>
        <span class="s1">&#39;code&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">],</span> <span class="n">drop</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">hq_codelist</span><span class="p">,</span> <span class="n">kz_codelist</span><span class="p">])</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">QA_fetch_get_stock_day</span><span class="p">(</span><span class="s1">&#39;000001&#39;</span><span class="p">,</span><span class="s1">&#39;2017-07-03&#39;</span><span class="p">,</span><span class="s1">&#39;2017-07-10&#39;</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">QA_fetch_get_stock_day</span><span class="p">(</span><span class="s1">&#39;000001&#39;</span><span class="p">,</span> <span class="s1">&#39;2013-07-01&#39;</span><span class="p">,</span> <span class="s1">&#39;2013-07-09&#39;</span><span class="p">))</span>
    <span class="c1">#print(QA_fetch_get_stock_realtime(&#39;000001&#39;))</span>
    <span class="c1">#print(QA_fetch_get_index_day(&#39;000001&#39;, &#39;2017-01-01&#39;, &#39;2017-07-01&#39;))</span>
    <span class="c1"># print(QA_fetch_get_stock_transaction(&#39;000001&#39;, &#39;2017-07-03&#39;, &#39;2017-07-10&#39;))</span>

    <span class="c1">#print(QA_fetch_get_stock_info(&#39;600116&#39;))</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>